package com.regulareport.web.controller.report;

import com.regulareport.common.annotation.Log;
import com.regulareport.common.core.controller.BaseController;
import com.regulareport.common.core.domain.AjaxResult;
import com.regulareport.common.core.page.TableDataInfo;
import com.regulareport.common.enums.BusinessType;
import com.regulareport.form.domain.*;
import com.regulareport.form.service.impl.ReportTaskService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/report/task")
@Api(tags = "计划管理")
public class ReportTaskController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(ReportTaskController.class);

    private final ReportTaskService reportTaskService;

    public ReportTaskController(ReportTaskService reportTaskService) {
        this.reportTaskService = reportTaskService;
    }

    /**
     * 获取下发任务列表
     */
    @PreAuthorize("@ss.hasPermi('report:task:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ReportTaskListReq req) {
        startPage();
        List<ReportTaskListRep> reportTask = reportTaskService.list(req);
        return getDataTable(reportTask);
    }

    /**
     * 获取下发任务列表
     */
    @PreAuthorize("@ss.hasPermi('report:task:listUser')")
    @PostMapping("/listUser")
    public TableDataInfo listUser(@RequestBody ReportTaskListReq req) {
        startPage();
        List<ReportTaskListRep> reportTask = reportTaskService.listUser(req);
        return getDataTable(reportTask);
    }

    /**
     * 删除人物
     */
    @PreAuthorize("@ss.hasPermi('report:task:delete')")
    @Log(title = "删除任务", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult del(@RequestBody Detail detail) {
        try {
            reportTaskService.delete(detail);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('report:task:detail')")
    @PostMapping("/detail")
    public AjaxResult detail(@RequestBody Detail detail) {
        try {
            ReportTaskRep reportTaskRep = reportTaskService.detail(detail);
            return AjaxResult.success(reportTaskRep);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查找任务
     */
    @PreAuthorize("@ss.hasPermi('report:task:find')")
    @PostMapping("/find")
    public AjaxResult find() {
        try {
            List<ReportTaskListRep> reportTask = reportTaskService.findUserTask(getLoginUser());
            return AjaxResult.success(reportTask);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 批量导出
     */
    @PreAuthorize("@ss.hasPermi('report:task:export')")
    @PostMapping("/multi/export")
    public void mutilExport(HttpServletResponse response, @RequestBody ReportVerifyReq req) {
        log.info("任务报表的批量导出方法mutilExport开始-----------------------------请求参数： {} ",  req.getTaskIds());
        try {
            reportTaskService.downloadXlsxFilesAsZip(req.getTaskIds(), response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 单元格数值调整
     */
    @PreAuthorize("@ss.hasPermi('report:task:update')")
    @Log(title = "报表单元格值调整", businessType = BusinessType.UPDATE)
    @PostMapping("/adjustCell")
    public AjaxResult adjustCell(@RequestBody ReportAdjustCellValueReq reportAdjustCellValueReq) {
        try {
            reportTaskService.adjustCellValue(reportAdjustCellValueReq);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
